package org.cjstudio.holypoopsgrs_protytpe;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("/helloworld")
public class HelloWorld {
	
	@GET 
    @Produces("text/plain")
    public String getIt() {
		String message;
		message = "Message to MySQL: Hello, MySQL!";
		message += "\nMessage from MySQL: ";
		
		String messageFromMySQL = null;
		
		try {
			String url="jdbc:mysql://localhost:3306/holypoopsgrs";           
			Class.forName("com.mysql.jdbc.Driver");
			
			Connection conn = DriverManager.getConnection(url, "root", "root");
			Statement stmt = conn.createStatement();
			String query = "select val from holypoopsgrs.test where id=1";
			
			ResultSet rs = stmt.executeQuery(query);
			System.out.println("Query : " + query);
			if(rs.first()) {
				messageFromMySQL = rs.getString(1);
				System.out.println("Result : " + messageFromMySQL);
			}
			stmt.close();
			conn.close();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		message += messageFromMySQL;
        return message;
    }
	
}
